package cn.schoolwow.quickdao.test.module.business.user.dml.json.flow.compare;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.dml.CompareJSONArrayResult;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.jsonarray.QueryJSONArray;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;

import java.util.List;

public class CompareUserJSONArrayFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setCurrentUserList(flowContext);
        synchronizedInstanceList(flowContext);
    }

    @Override
    public String name() {
        return "同步比较用户JSON列表";
    }

    private void setCurrentUserList(FlowContext flowContext){
        JSONArray currentUserArray = new JSONArray();
        for(int i=0;i<=5;i++){
            currentUserArray.add(getUserByIndex(i));
        }
        for(int i=20;i<=25;i++){
            currentUserArray.add(getUserByIndex(i));
        }
        flowContext.putTemporaryData("currentUserArray", currentUserArray);
    }

    private void synchronizedInstanceList(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        JSONArray currentUserArray = flowContext.checkData("currentUserArray", JSONArray.class);

        JSONArray userArray = dao.query(User.class).execute().getArray();

        CompareJSONArrayResult compareJSONArrayResult = dao
                .uniqueFieldNames("username")
                .compareJSONArray("user", currentUserArray, userArray);
        //要插入的
        {
            List<String> usernameList = QueryJSONArray.newQuery(compareJSONArrayResult.insertJSONArray)
                    .execute()
                    .getSingleFieldValueList("username");
            Assert.assertEquals(6, usernameList.size());
            for(int i=20;i<=25;i++){
                Assert.assertTrue(usernameList.contains("user"+i));
            }
        }
        //要更新的
        {
            List<String> usernameList = QueryJSONArray.newQuery(compareJSONArrayResult.updateJSONArray)
                    .execute()
                    .getSingleFieldValueList("username");
            Assert.assertEquals(6, usernameList.size());
            for(int i=0;i<=5;i++){
                Assert.assertTrue(usernameList.contains("user"+i));
            }
        }
        //要删除的
        {
            List<String> usernameList = QueryJSONArray.newQuery(compareJSONArrayResult.deleteJSONArray)
                    .execute()
                    .getSingleFieldValueList("username");
            Assert.assertEquals(5, usernameList.size());
            for(int i=6;i<=10;i++){
                Assert.assertTrue(usernameList.contains("user"+i));
            }
        }
    }

    private JSONObject getUserByIndex(int index){
        JSONObject user = new JSONObject();
        user.put("username", "user"+index);
        user.put("password", "123456");
        user.put("email", "user"+index+"@fake.com");
        user.put("mobile", "12345678911");
        return user;
    }
}